﻿package mortal.game.view.battlefield
{
    import com.gengine.core.*;
    import com.gengine.global.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import extend.language.*;
    import flash.display.*;
    import flash.events.*;
    import mortal.common.global.*;
    import mortal.game.manager.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.mvc.core.*;

    public class BaseMiniView extends View
    {
        protected var _bgName:String;
        protected var _titleText:String = "";
        protected var _bodySprite:Sprite;
        protected var _moveBtn:GLoadedButton;
        protected var _centerBg:ScaleBitmap;
        protected var _title:GTextFiled;
        protected var _openBtn:GLoadedButton;
        protected var _closeBtn:GLoadedButton;
        protected var _bg:Bitmap;
        protected var _isLoadingTypeBg:Boolean = false;

        public function BaseMiniView()
        {
            this.layer = LayerManager.uiLayer;
            return;
        }// end function

        override protected function configUI() : void
        {
            this._bodySprite = new Sprite();
            super.addChild(this._bodySprite);
            this._bodySprite.mouseEnabled = false;
            super.configUI();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            if (this._isLoadingTypeBg)
            {
                this._bg = UIFactory.gImageBitmap(this._bgName, 0, 0, this._bodySprite);
            }
            else
            {
                this._bg = UIFactory.bg(0, 0, 208, 351, this._bodySprite, this._bgName);
            }
            this._moveBtn = UIFactory.gLoadedButton(ImagesConst.TaskTrackMove_upSkin, 0, 3, 18, 18, this._bodySprite);
            this._moveBtn.toolTipData = HTMLUtil.addColor(Language.getString(20143), "#FFFFFF");
            this._centerBg = UIFactory.bg(18, 1, 200, 23, this._bodySprite, "channel_overSkin");
            this._centerBg.y = 1;
            this._centerBg.x = 18;
            this._title = UIFactory.gTextField(this._titleText, 18, 2, 200, 20, this, GlobalStyle.textFormatAnjin.center());
            this._closeBtn = UIFactory.gLoadedButton(ImagesConst.UpButton_upSkin, 0, 3, 18, 18);
            this._openBtn = UIFactory.gLoadedButton(ImagesConst.DownButton_upSkin, 0, 3, 18, 18);
            super.addChild(this._openBtn);
            super.addChild(this._closeBtn);
            this._openBtn.visible = false;
            this._moveBtn.configEventListener(MouseEvent.MOUSE_DOWN, this.onMoveBtnMouseHandler);
            this._openBtn.configEventListener(MouseEvent.CLICK, this.onShowHideHandler);
            this._closeBtn.configEventListener(MouseEvent.CLICK, this.onShowHideHandler);
            return;
        }// end function

        protected function onShowHideHandler(event:MouseEvent) : void
        {
            switch(event.currentTarget)
            {
                case this._openBtn:
                {
                    this._bodySprite.visible = true;
                    this._openBtn.visible = false;
                    this._closeBtn.visible = true;
                    break;
                }
                case this._closeBtn:
                {
                    this._bodySprite.visible = false;
                    this._openBtn.visible = true;
                    this._closeBtn.visible = false;
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        protected function onMoveBtnMouseHandler(event:MouseEvent) : void
        {
            startDrag();
            Global.stage.addEventListener(MouseEvent.MOUSE_UP, this.onStageMouseUpHandler);
            return;
        }// end function

        private function onStageMouseUpHandler(event:MouseEvent) : void
        {
            stopDrag();
            Global.stage.removeEventListener(MouseEvent.MOUSE_UP, this.onStageMouseUpHandler);
            return;
        }// end function

        override protected function updateView() : void
        {
            super.updateView();
            if (!isDisposed)
            {
                this.updateViewNow();
            }
            return;
        }// end function

        protected function updateViewNow() : void
        {
            this._centerBg.width = _width - 38;
            this._openBtn.x = _width - 20;
            this._closeBtn.x = _width - 20;
            this._title.width = _width - 38;
            if (!this._isLoadingTypeBg)
            {
                this._bg.width = _width;
                this._bg.height = _height;
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._moveBtn.dispose(param1);
            this._moveBtn = null;
            this._centerBg.dispose(param1);
            this._centerBg = null;
            this._openBtn.dispose(param1);
            this._openBtn = null;
            this._closeBtn.dispose(param1);
            this._closeBtn = null;
            if (this._bg is IDispose)
            {
                (this._bg as IDispose).dispose(param1);
                this._bg = null;
            }
            super.disposeImpl(param1);
            return;
        }// end function

        override public function addChild(param1:DisplayObject) : DisplayObject
        {
            return this._bodySprite.addChild(param1);
        }// end function

        override public function addChildAt(param1:DisplayObject, param2:int) : DisplayObject
        {
            return this._bodySprite.addChildAt(param1, param2);
        }// end function

    }
}
